﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using MVVMFramework.ViewModel;

namespace MVVMFramework.WPFDemo
{
    class UserVM : EditEntityViewModelBase<User>
    {
        public UserVM(User entity, Requests request)
            : base(entity, request)
        {

        }

        protected override bool Save(User entity)
        {
            try
            {
                AppModel.Instance.InsertUser(entity);
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(GetExceptionMessage(ex, false), "Unable to insert user", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            return false;
        }

        protected override string GetTitle()
        {
            if (Request == Requests.View)
                return string.Format("{0}", Entity.UserName);

            if (Request == Requests.Change)
                return "User will be changed";

            if (Request == Requests.Insert)
                return "User will be added";

            return "User";
        }

        protected override IEnumerable<IValidator> GetValidators(User entity)
        {
            List<IValidator> validators = new List<IValidator>();
            validators.Add(new NotNullOrEmptyValidator(() => Entity.UserName, "User name cannot be empty"));
            return validators;
        }

        protected override void OnValidationFailed(ValidationFailedEventArgs arg)
        {
            MessageBox.Show(GetFirstError());
            base.OnValidationFailed(arg);
        }
    }
}
